package com.qfhd.soft.cjzcore.UI.Dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.qfhd.soft.cjzcore.R;

public class CJNormalDialog {
    private AlertDialog dialog;

    private Context mContext;
    private Activity mActivity;

    public CJNormalDialog(Activity activity, Context context,String title,String message, View.OnClickListener listener){
        this.mActivity = activity;
        this.mContext = context;

        View dialogView = LayoutInflater.from(activity).inflate(R.layout.dialog_normal_layout, null);

        TextView TX_title        = dialogView.findViewById(R.id.dialog_normal_title);
        TextView TX_message      = dialogView.findViewById(R.id.dialog_normal_message);
        TextView sureBtn         = dialogView.findViewById(R.id.dialog_normal_sure);
        TextView cancelBtn       = dialogView.findViewById(R.id.dialog_normal_cancel);

        dialog = new AlertDialog.Builder(this.mActivity)
                .setCancelable(false)
                .setView(dialogView)
                .create();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        TX_title.setText(title);
        TX_message.setText(message);

        cancelBtn.setOnClickListener(v -> dismiss());
        sureBtn.setOnClickListener(v -> {
            if(listener != null){
                listener.onClick(sureBtn);
            }
            dismiss();
        });

        show();
    }

    public void CJNormalDialog(Context context,Activity activity,String title,String message, View.OnClickListener listener){
        View dialogView = LayoutInflater.from(activity).inflate(R.layout.dialog_normal_layout, null);

        TextView TX_title        = dialogView.findViewById(R.id.dialog_normal_title);
        TextView TX_message      = dialogView.findViewById(R.id.dialog_normal_message);
        TextView sureBtn         = dialogView.findViewById(R.id.dialog_normal_sure);
        TextView cancelBtn       = dialogView.findViewById(R.id.dialog_normal_cancel);

        TX_title.setText(title);
        TX_message.setText(message);

        cancelBtn.setOnClickListener(v -> dismiss());
        sureBtn.setOnClickListener(v -> {
            if(listener != null){
                listener.onClick(sureBtn);
            }
            dismiss();
        });

        show();
    }

    public void show() {
        dialog.show();
    }

    public void dismiss(){
        dialog.dismiss();
    }
}
